package com.jishuo.nontax.web;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.bean.Pagination;
import com.jishuo.base.bean.vo.UserVO;
import com.jishuo.base.config.AppConfig;
import com.jishuo.base.service.UserService;
import com.jishuo.base.util.StringUtil;
import com.jishuo.nontax.bean.bank.Item;
import com.jishuo.nontax.bean.bank.Unit;
import com.jishuo.nontax.bean.vo.*;
import com.jishuo.nontax.bfc.service.BaseDataService;
import com.jishuo.nontax.bfc.service.DailiBankService;
import com.jishuo.nontax.bfc.service.ItemService;
import com.jishuo.nontax.bfc.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 基础信息获取
 */
@Slf4j
@Controller
@RequestMapping(value = "/base/basedata", name = "基础信息")
public class BaseDataController {
    @Resource
    private BaseDataService baseDataService;
    @Resource
    private ItemService itemService;
    @Resource
    private UnitService unitService;
    @Resource
    private UserService userService;

    // 页面文件位置
    private String prefix = "base/";

    @RequestMapping(value = {"bankindex", "bankindex/{page}"}, name = "账户页面")
    public String bankindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "bankindex" : page);
    }

    @RequestMapping(value = {"cantonindex", "cantonindex/{page}"}, name = "行政区划页面")
    public String cantonindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "cantonindex" : page);
    }

    @RequestMapping(value = {"unitindex", "unitindex/{page}"}, name = "预算单位页面")
    public String unitindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "unitindex" : page);
    }

    @RequestMapping(value = {"itemindex", "itemindex/{page}"}, name = "收费项目界面")
    public String itemindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "itemindex" : page);
    }

    @RequestMapping(value = {"bankstationindex", "bankstationindex/{page}"}, name = "银行网点界面")
    public String bankstationindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "bankstationindex" : page);
    }

    @RequestMapping(value = {"paywayindex", "paywayindex/{page}"}, name = "缴款方式界面")
    public String paywayindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "paywayindex" : page);
    }

    @RequestMapping(value = {"billtypeindex", "billtypeindex/{page}"}, name = "票据种类界面")
    public String billtypeindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "billtypeindex" : page);
    }

    @RequestMapping(value = {"bankreturnindex", "bankreturnindex/{page}"}, name = "银行网点界面")
    public String bankreturnindex(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "bankreturnindex" : page);
    }


    /**
     * @author hlp
     * @date 2023/3/2 14:22
     * @Description: 获取区域划分数据
     */
    @ResponseBody
    @RequestMapping(value = "findUserCanton", name = "区划")
    public List<CantonVO> findUserCanton(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return baseDataService.findCantonView(user);
    }

    @ResponseBody
    @RequestMapping(value = "findAllCantonMap", name = "全部区划")
    public List<AreaVO> findCantonAll() {
        return baseDataService.findAllCantonMap();
    }

    @ResponseBody
    @RequestMapping(value = "findCantonMapInStation", name = "网点区划")
    public List<AreaVO> findCantonMapInStation() {
        return baseDataService.findCantonMapInStation();
    }

    @ResponseBody
    @RequestMapping(value = "findUserCantonMap", name = "区划")
    public List<AreaVO> findUserCantonMap(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return baseDataService.findCantonMapView(user);
    }

    /**
     * @author hlp
     * @date 2023/3/3 10:21
     * @Description:  获取项目数据
     */
    @ResponseBody
    @RequestMapping(value = "findItem", name = "项目")
    public List<ItemVO>  findItem(Long cantonid, Long unitid) {
        return itemService.findItem(cantonid, unitid);
    }


    /**
     * @author hlp
     * @date 2023/3/3 10:20
     * @Description:  获取单位数据
     */
    @ResponseBody
    @RequestMapping(value = "findUnit", name = "项目")
    public List<UnitVO> findUnit(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return unitService.findUnitView(user.getCantonId());
    }

    @ResponseBody
    @RequestMapping(value = "findUnitCantonId", name = "项目")
    public List<UnitVO> findUnitCantonId(Long cantonid) {
        return unitService.findUnitView(cantonid);
    }

    @ResponseBody
    @RequestMapping(value = "findUnitTwo", name = "项目")
    public List<Unit> findUnitTwo(Long cantonid) {
        return unitService.findUnitViewTwo(cantonid);
    }


    @ResponseBody
    @RequestMapping(value = "findBankVoView", name = "")
    public List<BankVO> findBankVoView(ChargeBillVO chargeBillVO){
        return baseDataService.findBankVoView(chargeBillVO.getCantonid());
    }

    /**
     * @author hlp
     * @date 2023/3/3 11:40
     * @Description: 获取银行数据
     */
    @ResponseBody
    @RequestMapping(value = "findBank", name = "获取银行数据")
    public List<BankVO> findBank(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return baseDataService.findBankView(user);
    }

    /**
     * @author hlp
     * @date 2023/3/17 14:48
     * @Description: 根据区划获取单位数据
     */
    @ResponseBody
    @RequestMapping(value = "findCantonIdUnit", name = "根据区划获取单位数据")
    public List<UnitVO> findCantonIdUnit(String cantonid) {
        Long canid= (cantonid!=null&&cantonid!="")?Long.valueOf(cantonid):null;
        return baseDataService.findUnitPage(canid,new Pagination());
    }

    /**
     * @author hlp
     * @date 2023/3/16 17:34
     * @Description: 基础信息账户分页
     */
    @ResponseBody
    @RequestMapping(value = "findBankCantonid", name = "基础信息账户分页")
    public GridResult<BankVO> findBankCantonid(BankVO bankVO, Pagination pagination) {
        return new GridResult<>(baseDataService.findBankPage(bankVO.getCantonid(),pagination));
    }
    @ResponseBody
    @RequestMapping(value = "findBankList", name = "银行账户")
    public List<AreaVO> findBankList(BankVO bankVO) {
        return baseDataService.findBankList(bankVO.getCantonid());
    }


    @ResponseBody
    @RequestMapping(value = "findBankBystatePage", name = "获取银行数据分页")
    public GridResult<BankVO> findBankBystatePage(HttpSession session) {
        UserVO user = (UserVO) session.getAttribute(AppConfig.LOGIN_USER_SESSION_KEY);
        return new GridResult<>(baseDataService.findBankBystatePage(user));
    }



    @ResponseBody
    @RequestMapping(value = "findBillTypePage", name = "票据种类分页")
    public GridResult<BillTypeVO> findBillTypePage(Pagination pagination) {
        return new GridResult<>(baseDataService.findBillTypePage(pagination));
    }


    @Resource
    private DailiBankService dailiBankService;

    @ResponseBody
    @RequestMapping(value = "getbank", name = "主动获取银行")
    public BaseResult getbank(Long cantonid,String accountcode){
        if (cantonid == null || StringUtil.isEmpty(accountcode)) return new BaseResult(ErrorCodeEnum.PARAM_ERR);
        dailiBankService.getDailiBank(cantonid,accountcode);
        return new BaseResult();
    }

    /**
     * @author hlp
     * @date 2023/3/17 11:35
     * @Description: 预算单位列表
     */
    @ResponseBody
    @RequestMapping(value = "findUnitPage", name = "预算单位列表")
    public GridResult<UnitVO>  findUnitPage(UnitVO unitVO, Pagination pagination) {
        return new GridResult<>(baseDataService.findUnitPage(unitVO.getCantonid(),pagination));
    }

    @ResponseBody
    @RequestMapping(value = "findPayWayPage", name = "预算单位列表")
    public GridResult<PayWayVO> findPayWayPage(Pagination pagination) {
        return new GridResult<>(baseDataService.findPayWayPage(pagination));
    }


    @ResponseBody
    @RequestMapping(value = "findItemPage", name = "预算单位列表")
    public GridResult<ItemVO> findItemPage(ItemVO itemVO, Pagination pagination) {
        return new GridResult<>(baseDataService.findItemPage(itemVO.getCantonid(),itemVO.getUnitid(),pagination));
    }

    /**
     * @author hlp
     * @date 2023/3/17 10:39
     * @Description: 行政区划列表
     */
    @ResponseBody
    @RequestMapping(value = "findCantonPage", name = "行政区划列表")
    public GridResult<CantonVO> findCantonPage(Pagination pagination) {
        return new GridResult<>(baseDataService.findCantonPage(pagination));
    }

    // -------------------------------网点开始------------------
    @ResponseBody
    @RequestMapping(value = "findBankStationPage", name = "网点列表")
    public GridResult<BankStationVO> findBankStationPage(BankStationVO bankStationVO,Pagination pagination) {
        return new GridResult<>(baseDataService.findBankStationPage(bankStationVO.getCantonid(),pagination));
    }

    @ResponseBody
    @RequestMapping(value = "saveBankStation", name = "添加银行网点")
    public BaseResult saveBankStation(BankStationVO bankStationVO) {
        return baseDataService.saveBankStation(bankStationVO);
    }

    @ResponseBody
    @RequestMapping(value = "updateBankStation", name = "修改银行网点")
    public BaseResult updateBankStation(BankStationVO bankStationVO) {
        return baseDataService.updateBankStation(bankStationVO);
    }

    @ResponseBody
    @RequestMapping(value = "deleteBankStation", name = "删除银行网点")
    public BaseResult deleteBankStation(long bankstationid) {
        return baseDataService.deleteBankstation(bankstationid);
    }

    @ResponseBody
    @RequestMapping(value = "sendBankStation", name = "发送银行信息")
    public BaseResult sendBankStation(String billids, String usercode, String userpwd, HttpSession session){
        UserVO user = (UserVO) session.getAttribute(AppConfig.LOGIN_USER_SESSION_KEY);
        BaseResult ret = userService.chkAuthUser(usercode, userpwd, user);
        if (!ret.isSuccess()) return ret;
        long bankstationid = Long.parseLong(billids);
        return baseDataService.sendBankStationById(bankstationid);
    }

    /**
     * @author hlp
     * @date 2023/3/3 11:40
     * @Description: 获取收款网点数据
     */
    @ResponseBody
    @RequestMapping(value = "findBankStation", name = "获取收款网点数据")
    public List<BankStationVO> findBank(Long cantonid) {
        return baseDataService.findBankStationView(cantonid);
    }

    // -------------------------------网点结束------------------

    /**
     * @author hlp
     * @date 2023/3/17 11:28
     * @Description: 主动获取预算单位
     */
    @ResponseBody
    @RequestMapping(value = "getBaseUnit", name = "主动获取预算单位")
    public BaseResult getBaseUnit(int cantonid,String unitcode){
        return unitService.getBaseUnit(cantonid, unitcode);
    }

    @ResponseBody
    @RequestMapping(value = "getNewItem", name = "主动获取项目")
    public BaseResult getNewItem(String itemcode){
        return itemService.getNewItem(itemcode);
    }

    /**
     * @author hlp
     * @date 2023/3/3 11:40
     * @Description: 获取缴款方式
     */
    @ResponseBody
    @RequestMapping(value = "findPayWay", name = "获取缴款方式")
    public List<PayWayVO> findPayWay() {
        return baseDataService.findPayWayView();
    }

    @ResponseBody
    @RequestMapping(value = "findUnitVsItemView", name = "")
    public List<Item> findUnitVsItemView(Long unitid) {
        return itemService.findUnitVsItemView(unitid);
    }


    @ResponseBody
    @RequestMapping(value = "findUnidVsItem")
    public List<Item> findUnitVsItem(Long unitid){
        return  baseDataService.findUnitVsItem(unitid);
    }


    @ResponseBody
    @RequestMapping(value = "returnBank")
    public BaseResult returnBank(BankVO bankVO){
        return  baseDataService.confirmBankManage(bankVO);
    }


    @ResponseBody
    @RequestMapping(value = "findBankStationCan", name = "网点列表")
    public List<BankStationVO> findBankStationPage(String cantoncode) {
        return baseDataService.findStationCancode(cantoncode);
    }

}
